<?php

namespace app\health\service;

use app\health\model\HealthConfig;
use think\Cache;

class ConfigService
{
    /**
     * 获取所有配置
     * @param boolean $isForce 是否强制刷新缓存
     * @return array
     * @Author 张旭鹏
     * @DateTime 2023-07-03
     */
    public static function getConfig(bool $isForce = false): array
    {
        $config = Cache::get('health_config');
        if (!$config || $isForce === true) {
            $config = HealthConfig::column('value', 'name');
            array_walk($config, static function (&$value, $key) {
                $value = json_decode($value, true);
            });
            Cache::set('health_config', $config);
        }
        return $config;
    }

    /**
     * 获取用户配置
     * @param string $var
     * @Author 张旭鹏
     * @DateTime 2023-07-03
     */
    public static function getUserConfig($var = '')
    {
        $config = self::getConfig()['user'] ?? [];
        if ($var) {
            return $config[$var] ?? '';
        }
        return $config;
    }

    /**
     * 获取提现配置
     * @return array
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public static function getWithdrawConfig(): array
    {
        return self::getConfig()['withdraw'] ?? [];
    }

    /**
     * 微信小程序参数
     * @return array
     * @Author 张旭鹏
     * @DateTime 2023-06-15
     */
    public static function getWeMiniConfig(): array
    {
        return [
            'appid'     => 'wxee55df52d0c7673f',
            'appsecret' => 'bb3a5f76c2c2e6a73774eca32091ef5f'
        ];
    }

    /**
     * 获取微信支付参数
     * @return array
     * @Author 张旭鹏
     * @DateTime 2023-06-15
     */
    public static function getWechatPayConfig(): array
    {
        return [
            'appid'   => self::getWeMiniConfig()['appid'],
            'mch_id'  => '1613954729',
            'mch_key' => 'xiaoxinchongdian20210908lanerxzp',
            // 配置商户支付双向证书目录（可选，在使用退款|打款|红包时需要）
            'ssl_key' => '',
            'ssl_cer' => '',
        ];
    }

    /**
     * 签到规则
     * @return array
     * @Author 张旭鹏
     * @DateTime 2023-06-25
     */
    public static function signInRuleConfig(): array
    {
        return [
            'reward_type' => 2,   //0=无奖励 1=余额 2健康币
            'reward'      => 1,   //奖励数值
            'coiled'      => [
                [
                    'day'         => 2,
                    'reward_type' => 2,
                    'reward'      => 2
                ],
                [
                    'day'         => 3,
                    'reward_type' => 2,
                    'reward'      => 3
                ],
                [
                    'day'         => 4,
                    'reward_type' => 2,
                    'reward'      => 4
                ],
                [
                    'day'         => 5,
                    'reward_type' => 2,
                    'reward'      => 5
                ],
                [
                    'day'         => 6,
                    'reward_type' => 2,
                    'reward'      => 6
                ],
                [
                    'day'         => 7,
                    'reward_type' => 1,
                    'reward'      => ['0.01', '0.02']
                ],
            ]
        ];
    }

    /**
     * 获取商城订单配置信息
     * @Author 张旭鹏
     * @DateTime 2023-06-30
     */
    public static function getOrderConfig(): array
    {
        $config = self::getConfig()['order'] ?? [];
        return [
            'order_auto_close'   => ($config['order_auto_close'] ?? 1440) * 60,     //自动关闭订单时间
            'order_auto_confirm' => ($config['order_auto_confirm'] ?? 7) * 86400,   //自动确认收货
        ];
    }

    /**
     * 健康+配置
     * @return array|int[]
     */
    public static function getHealthPlusConfig(): array
    {
        $config = self::getConfig()['health_plus'] ?? [];
        return [
            'goal_step' => $config['goal_step'] ?? 10000,//目标步数
        ];
    }
}
